/**   
* @Title: WebGoodsController.java 
* @Package com.txby.platformApi.component.web 
* @Description: 商城商品控制层
* @author Administrator
* @date 2017年11月2日 下午4:07:49 
* @version V1.0   
*/
package com.txby.platformApi.component.web;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jck.mybatis.util.PageList;
import com.jck.tools.ChkUtil;
import com.txby.pf.common.utils.PageUtils;
import com.txby.pf.common.utils.Query;
import com.txby.pf.common.utils.R;
import com.txby.pf.common.validator.ValidatorUtils;
import com.txby.pf.common.validator.group.AddGroup;
import com.txby.platformApi.component.service.WebOrderMealGoodsService;
import com.txby.platformCore.component.util.Chinese2PinyinUtil;
import com.txby.platformCore.entity.shop.OrderMealGoods;

/** 
* @ClassName: WebNrsGoodsController 
* @Description: 商城商品类 
* @author Administrator
* @date 2017年11月2日 下午4:07:49 
*  
*/
@RestController
@RequestMapping("/web/ordermealgoods")
public class WebOrderMealGoodsController extends CommonController {

	@Resource
	public WebOrderMealGoodsService webOrderMealGoodsService;
	
	@RequestMapping("/list")
	@RequiresPermissions("sp:goods:info")
	public R list(@RequestParam Map<String, Object> params){
//		数据隔离
		String sql=getSql();
		String name=params.get("name")==null?"":params.get("name").toString();
		if(!ChkUtil.isEmptyAllObject(name)){
			if (name.contains("%") || name.contains("_")) {
				name = name.trim().replace("%", "\\%").replace("_", "\\_");
				name = "%" + name + "%";
				sql += " and bg.name like #{pojo.name} escape '\\'";
				params.put("name", "%" + name.trim() + "%");
			} else {
				name = "%" + name.trim() + "%";
				sql += " and bg.name like #{pojo.name} ";
				params.put("name", "%" + name.trim() + "%");
			}
		}
		sql += Query.getLastSortSql(params, " order by bg.updateTime ");
		//查询列表数据
		PageList<Map> pl = this.mBaseDao.findBySqlForPageList(sql, Map.class, Query.getPageBean(params), params);
		PageUtils pageUtil = new PageUtils(pl.getResultList(), pl.getPageBean().getTotalCount().intValue(),  pl.getPageBean().getPageSize(), pl.getPageBean().getPage());
		return R.ok().put("page", pageUtil);
	}
	
	/**
	 * 
	* @Title: getShopsList 
	* @Description: 获取商家 
	* @return R    返回类型 
	* @throws
	 */
	@RequestMapping("/getShopsList")
	public R getShopsList(){
		String sql="SELECT t.id_ shopsId,t.name shopName  FROM  t_txby_sp_shops t,sys_dept d where t.deptId=d.dept_id and t.status=0 and d.dept_id="+getDeptId();
		List list=mBaseDao.findBySql(sql, Map.class, null);
		return R.ok().put("list", list);
		
	}
	
	/**
	 * 
	 * @Title: getMealTypeList 
	 * @Description: 获取餐别 
	 * @return R    返回类型 
	 * @throws
	 */
	@RequestMapping("/getMealTypeList")
	public R getMealTypeList(){
		String sql="select m.id_ mealTypeId,m.mealName from t_txby_sp_mealType m,t_txby_sp_shops s where m.shopId=s.id_ and s.deptId="+getDeptId();
		List list=mBaseDao.findBySql(sql, Map.class, null);
		return R.ok().put("list", list);
		
	}
	
	
	/**
	 * 查询信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sp:goods:info")
	public R info(@PathVariable("id") String id){
		String sql=getSql();
	    sql=sql+ " and bg.id_='"+id+"'" ;
		Map goods =  (Map) this.mBaseDao.findBySqlUniqueObject(sql, Map.class, null);
		return R.ok().put("goods", goods);
	}
	
	private String getSql() {
		String sql="select bg.id_,bg.goodsTypeId,bg.keyName,bg.introDetail,bg.introNotice,bg.introState,bg.publishTime,bg.downTime,bg.isDown,bg.isDown sellStatus,bg.name,bg.price,bg.shopsId,s.name shopsName, " 
				 + "n.id_ stockId,n.stockNum,n.residueNum,n.ordersOutNum,r.goodsPicId,pic.url from t_txby_sp_orderMealGoods bg join t_txby_sp_goodsStockNum n ON "
				 + " bg.id_ = n.goodsId and bg.goodsTypeId=n.goodsTypeId and bg.shopsId=n.shopsId "
				 + " join t_txby_sp_goodsPicRealation r on bg.id_=r.goodsId and bg.goodsTypeId=r.goodsTypeId JOIN "
				+ " t_txby_sp_goodsPic pic on r.goodsPicId=pic.id_ join t_txby_sp_shops s on bg.shopsId=s.id_ where 1=1 ";
		return sql;
	}
	/**
	 * 保存库存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sp:goods:save")
	public R save(@RequestBody Map map){
		try {
			OrderMealGoods goods = new OrderMealGoods();
			String goodsTypeId = map.get("goodsTypeId")==null?"":map.get("goodsTypeId")+"";
			goods.setGoodsTypeId(Long.valueOf(goodsTypeId));
			String shopsId = map.get("shopsId")==null?"":map.get("shopsId")+"";
			if(StringUtils.isBlank(shopsId)) {
				goods.setShopsId(null);
			} else {
				goods.setShopsId(Long.valueOf(shopsId));
			}
			String introDetail = map.get("introDetail")==null?"":map.get("introDetail")+"";
			goods.setIntroDetail( introDetail);
			String introState = map.get("introState")==null?"":map.get("introState")+"";
			goods.setIntroState(introState);
			String introNotice = map.get("introNotice")==null?"":map.get("introNotice")+"";
			goods.setIntroNotice(introNotice);
			String price = map.get("price")==null?"":map.get("price")+"";
			goods.setPrice(Double.valueOf(price));
			String NO = map.get("NO")==null?"":map.get("NO")+"";
			goods.setNO(NO);
			String name0 = map.get("name")==null?"":map.get("name")+"";
			goods.setName(name0);
			String keyName = map.get("keyName")==null?"":map.get("keyName")+"";
			goods.setKeyName(keyName);
			String stockNum = map.get("stockNum")==null?"":map.get("stockNum")+"";
			goods.setStockNum(Long.valueOf(stockNum));
			ValidatorUtils.validateEntity(goods);
			String sql="select * from "+this.mBaseDao.tableName(OrderMealGoods.class)+" where name='"+goods.getName()+"' and shopsId='"+goods.getShopsId()+"' and goodsTypeId='"+goods.getGoodsTypeId()+"'";
			OrderMealGoods old=(OrderMealGoods) this.mBaseDao.findBySqlUniqueObject(sql, OrderMealGoods.class, null);
			 if(old!=null){
				 return R.ok().error("该商品已存在");
			 }
			 goods.setCreater(getUserId().toString());
			 goods.setUpdater(getUserId().toString());
			String name = goods.getName();
			String spellFullName = Chinese2PinyinUtil.getPingYin(name);
			String spellSimpleName = Chinese2PinyinUtil.getPinYinHeadChar(name);
			goods.setSpellFullName(spellFullName);
			goods.setSpellSimpleName(spellSimpleName);
			goods.setCreateTime(new Date());
			goods.setCreateTime(new Date());
			goods.setStatus(0);
			goods.setIsDown(0);
			goods.setPublishTime(new Date());
			webOrderMealGoodsService.saveOrderMealGoods(goods,map);
		} catch (Exception e) {
			return R.error(5000,e.getMessage());
		}
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sp:goods:update")
	public R update(@RequestBody  OrderMealGoods goods){
		try {
			ValidatorUtils.validateEntity(goods, AddGroup.class);
			String sql="select * from "+this.mBaseDao.tableName(OrderMealGoods.class)+" where typeNo ="+goods.getName()+" and id_!="+goods.getId_()+" ";
			OrderMealGoods old=(OrderMealGoods) this.mBaseDao.findBySqlUniqueObject(sql, OrderMealGoods.class, null);
			if(old!=null){
				return R.ok().error("该商品已存在");
			}else{
				old=(OrderMealGoods) this.mBaseDao.findBySqlUniqueObject("select * from "+this.mBaseDao.tableName(OrderMealGoods.class)+" where   id_="+goods.getId_(), OrderMealGoods.class, null);
				if(old!=null){
					this.mBaseDao.updateObject(goods);
				}
			}
			
		}catch (Exception e) {
			return R.error(5000,e.getMessage());
		}
		return R.ok();
	}
	
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sp:goods:delete")
	public R delete(@RequestBody String[] ids){
		 for(String id:ids){
		    	this.mBaseDao.executeSql(" update  "+this.mBaseDao.tableName(OrderMealGoods.class)+" set status=1 where id='"+id+"'", null);
		  }
		return R.ok();
	}
}
